//
//  QXShuJuViewController.m
//  qiJi
//
//  Created by 凤利史 on 2018/4/4.
//  Copyright © 2018年 sunrose. All rights reserved.
//

#import "QXShuJuViewController.h"
#import "HealthKitManage.h"

#import <AMapFoundationKit/AMapFoundationKit.h>

#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

@interface QXShuJuViewController ()<AMapLocationManagerDelegate,AMapSearchDelegate>
{
    
    NSInteger panDuanDiYiCi;//判断是不是第一次进来获取初始点经纬度
    NSString *qiDianZuoBiaoLat;
    NSString *qiDianZuoBiaoLon;
    
    NSString *zhongDianZuoBiaoLat;
    NSString *zhongDianZuoBiaoLon;
    
    //距离
    NSString *routeDistace;

    
}
@property (nonatomic, strong) UIButton *zanTingButton;//暂停button
@property (nonatomic, strong) UIButton *dingWeiButton;//定位
@property (nonatomic, strong) UIButton *zhaoXiangButton;//相册
@property (nonatomic, strong) UIButton *kaishiButton;//开始
@property (nonatomic, strong) UIButton *jieShuButton;//结束
@property (nonatomic, strong) UILabel *juLiLab;//距离
@property (nonatomic, strong) UILabel *yunDongShiJian;//运动时间
@property (nonatomic, strong) UILabel *pingJunSuDu;//平均速度

@property (nonatomic, strong)AMapLocationManager *locationManager;
//搜索
@property (nonatomic, strong) AMapSearchAPI *search;

//起点坐标
@property (nonatomic) CLLocationCoordinate2D startCoordinate;
//终点坐标
@property (nonatomic) CLLocationCoordinate2D destinationCoordinate;


@end

@implementation QXShuJuViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    
}
//设置顶部20的部分字体颜色变为白色
- (UIStatusBarStyle)preferredStatusBarStyle{

    return UIStatusBarStyleLightContent;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    panDuanDiYiCi = 1;
    
    self.view.backgroundColor = getColor(@"7c8187");
    
    //设置窗口亮度大小  范围是0.1 -1.0
    [[UIScreen mainScreen] setBrightness:0.5];
    //设置屏幕常亮
    [UIApplication sharedApplication].idleTimerDisabled = YES;
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    [self creationView];
    [self gaoDeDiTu];
    
    // Do any additional setup after loading the view.
}
- (void)gaoDeDiTu{
    
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 20;
    
    [self.locationManager startUpdatingLocation];
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    [self seZhiQiXing];
    
}
- (void)seZhiQiXing{
    
    float qiDianLat = [qiDianZuoBiaoLat floatValue];
    float qiDianLon = [qiDianZuoBiaoLon floatValue];
    float zhongDianLat = [zhongDianZuoBiaoLat floatValue];
    float zhongDianLon = [zhongDianZuoBiaoLon floatValue];
    _startCoordinate = CLLocationCoordinate2DMake(qiDianLat, qiDianLon);
    _destinationCoordinate = CLLocationCoordinate2DMake(zhongDianLat, zhongDianLon);
    
    
//    AMapRidingRouteSearchRequest *navi = [[AMapRidingRouteSearchRequest alloc] init];
//    navi.origin = [AMapGeoPoint locationWithLatitude:self.startCoordinate.latitude longitude:self.startCoordinate.longitude];
//    navi.destination = [AMapGeoPoint locationWithLatitude:self.destinationCoordinate.latitude longitude:self.destinationCoordinate.longitude];
//    [self.search AMapRidingRouteSearch:navi];
    
    
    
    
}
- (void)creationView{
    
    
    
    self.juLiLab = [[UILabel alloc] init];
    [self.view addSubview:_juLiLab];
    [_juLiLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(100);
        make.centerX.equalTo(self.view);
        make.height.offset(55);
    }];
    //_juLiLab.text = @"222";
    _juLiLab.textColor = [UIColor whiteColor];
    _juLiLab.font = [UIFont fontWithName:@"DINEngschrift LT Alternate" size:50];
//    HealthKitManage *manage = [HealthKitManage shareInstance];
//    [manage authorizeHealthKit:^(BOOL success, NSError *error) {
//
//        if (success) {
//            NSLog(@"success");
//            [manage getDistance:^(double value, NSError *error) {
//                NSLog(@"2count-->%.2f", value);
//                NSLog(@"2error-->%@", error.localizedDescription);
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    _juLiLab.text = [NSString stringWithFormat:@"%.2f", value];
//                });
//
//
//                
//            }];
//        }
//        else {
//            NSLog(@"fail");
//        }
//    }];
    
   
    UILabel *labelGongLi = [[UILabel alloc] init];
    [self.view addSubview:labelGongLi];
    [labelGongLi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.juLiLab.mas_bottom).with.offset(15);
        make.centerX.equalTo(self.view);
        
        
    }];
    labelGongLi.text = @"距离 (公里)";
    labelGongLi.textColor = [UIColor whiteColor];
    labelGongLi.font = [UIFont fontWithName:@"DINEngschrift LT Alternate" size:15];
    self.yunDongShiJian = [[UILabel alloc] init];
    [self.view addSubview:self.yunDongShiJian];
    self.yunDongShiJian.textAlignment = NSTextAlignmentCenter;
    
    [self.yunDongShiJian mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelGongLi.mas_bottom).with.offset(70);
        make.left.equalTo(self.view);
        make.width.offset(DEF_SCREEN_WIDTH/2);
        
    }];
    self.yunDongShiJian.textColor = [UIColor whiteColor];
    self.yunDongShiJian.text = @"00.00.00";
    _yunDongShiJian.font = [UIFont fontWithName:@"DINEngschrift LT Alternate" size:25];
    
    
    UILabel *yunD = [[UILabel alloc] init];
    [self.view addSubview:yunD];
    yunD.textAlignment = NSTextAlignmentCenter;
    [yunD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_yunDongShiJian.mas_bottom).with.offset(15);
        make.centerX.equalTo(_yunDongShiJian);
        
        
        
    }];
    yunD.text = @"运动时间";
    yunD.textColor = [UIColor whiteColor];
    yunD.font = [UIFont fontWithName:@"DINEngschrift LT Alternate" size:15];
    
    UIView *view1 = [[UIView alloc] init];
    [self.view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(_yunDongShiJian.mas_top).with.offset(15);
        make.width.offset(1.5);
        make.height.offset(30);
    }];
    view1.backgroundColor = [UIColor whiteColor];
    self.pingJunSuDu = [[UILabel alloc] init];
    [self.view addSubview:self.pingJunSuDu];
    self.pingJunSuDu.textAlignment = NSTextAlignmentCenter;
    
    [self.pingJunSuDu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labelGongLi.mas_bottom).with.offset(70);
        make.right.equalTo(self.view);
        make.width.offset(DEF_SCREEN_WIDTH/2);
        
    }];
    self.pingJunSuDu.textColor = [UIColor whiteColor];
    self.pingJunSuDu.text = @"00";
    _pingJunSuDu.font = [UIFont fontWithName:@"DINEngschrift LT Alternate" size:25];
    
    UILabel *pingJunSD = [[UILabel alloc] init];
    [self.view addSubview:pingJunSD];
    pingJunSD.textAlignment = NSTextAlignmentCenter;
    [pingJunSD mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_pingJunSuDu.mas_bottom).with.offset(15);
        make.centerX.equalTo(_pingJunSuDu);
        
        
        
    }];
    pingJunSD.text = @"平均速度";
    pingJunSD.textColor = [UIColor whiteColor];
    pingJunSD.font = [UIFont fontWithName:@"DINEngschrift LT Alternate" size:15];
    
    
    
    
    
  ////////////////////////////////////////
    //暂停
    self.zanTingButton = [[UIButton alloc] init];
    [self.view addSubview:self.zanTingButton];
    [self.zanTingButton setBackgroundImage:[UIImage imageNamed:@"riding_suspeng_btn"] forState:UIControlStateNormal];
    self.zanTingButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2-50*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 70*CHANGE_SIZE - 100*CHANGE_SIZE, 100*CHANGE_SIZE, 100*CHANGE_SIZE);
   // self.zanTingButton.backgroundColor = [UIColor whiteColor];
    [self.zanTingButton addTarget:self action:@selector(dongHuaClick) forControlEvents:UIControlEventTouchUpInside];
    self.zanTingButton.alpha = 1;
    //定位
    
    self.dingWeiButton = [[UIButton alloc] init];
    [self.view addSubview:self.dingWeiButton];
    [self.dingWeiButton setBackgroundImage:[UIImage imageNamed:@"riding_map_btn"] forState:UIControlStateNormal];
    self.dingWeiButton.frame = CGRectMake(45*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 90*CHANGE_SIZE - 60*CHANGE_SIZE, 60*CHANGE_SIZE, 60*CHANGE_SIZE);
   // self.dingWeiButton.backgroundColor = [UIColor whiteColor];
    [self.dingWeiButton addTarget:self action:@selector(dingWeiClick) forControlEvents:UIControlEventTouchUpInside];
    //相册
    self.zhaoXiangButton = [[UIButton alloc] init];
    [self.view addSubview:self.zhaoXiangButton];
    [self.zhaoXiangButton setBackgroundImage:[UIImage imageNamed:@"riding_photo_btn"]forState:UIControlStateNormal];
    self.zhaoXiangButton.frame = CGRectMake(DEF_SCREEN_WIDTH- 45*CHANGE_SIZE-60*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 90*CHANGE_SIZE - 60*CHANGE_SIZE, 60*CHANGE_SIZE, 60*CHANGE_SIZE);
   // self.zhaoXiangButton.backgroundColor = [UIColor whiteColor];
    [self.zhaoXiangButton addTarget:self action:@selector(dingWeiClick) forControlEvents:UIControlEventTouchUpInside];
    
    //开始
    self.kaishiButton = [[UIButton alloc] init];
    [self.view addSubview:self.kaishiButton];
    [self.kaishiButton setBackgroundImage:[UIImage imageNamed:@"riding_start_btn"]forState:UIControlStateNormal];
    //DEF_SCREEN_WIDTH/2-50*CHANGE_SIZE 87*CHANGE_SIZE
    self.kaishiButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2-40*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 80*CHANGE_SIZE - 80*CHANGE_SIZE, 80*CHANGE_SIZE, 80*CHANGE_SIZE);
    self.kaishiButton.alpha = 0;
    
   // self.kaishiButton.backgroundColor = [UIColor whiteColor];
    [self.kaishiButton addTarget:self action:@selector(kaishiClick) forControlEvents:UIControlEventTouchUpInside];
    //结束
    
    self.jieShuButton = [[UIButton alloc] init];
    [self.view addSubview:self.jieShuButton];
    //
    [self.jieShuButton setBackgroundImage:[UIImage imageNamed:@"riding_finish_btn"]forState:UIControlStateNormal];
    self.jieShuButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2, DEF_SCREEN_HEIGHT - 80*CHANGE_SIZE - 80*CHANGE_SIZE, 80*CHANGE_SIZE, 80*CHANGE_SIZE);
    self.jieShuButton.alpha = 0;
    
   // self.jieShuButton.backgroundColor = [UIColor whiteColor];
    [self.jieShuButton addTarget:self action:@selector(jieShuClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
}
#pragma mark 处理位置更新。
- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
    if (panDuanDiYiCi == 1) {

        qiDianZuoBiaoLat = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        qiDianZuoBiaoLon = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
        
        zhongDianZuoBiaoLat = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        zhongDianZuoBiaoLon = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        
        
    }else{
        
        zhongDianZuoBiaoLat = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
        zhongDianZuoBiaoLon = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
        
    }
    panDuanDiYiCi++;
    
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}
/* 路径规划搜索回调. */
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    if (response.route == nil)
    {
        return;
    }
    
    
    if (response.count > 0)
    {
        
        AMapPath *path = response.route.paths[0];
        routeDistace = [NSString stringWithFormat:@"%.2f",path.distance/1000.00 ];
        NSString *str = [NSString stringWithFormat:@"%@",routeDistace];
        _juLiLab.text = str;
        
        if ([routeDistace floatValue] == 0) {
            _juLiLab.text = @"";
        }
    }
    
    
    
    //解析response获取路径信息，具体解析见 Demo
}

#pragma  mark 当检索失败时，会进入 didFailWithError 回调函数，通过该回调函数获取产生的失败的原因。
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", error);
}





//暂停
- (void)dongHuaClick{
     NSLog(@"2222222");
    [UIView animateWithDuration:0.3 animations:^{
        
        self.zanTingButton.alpha = 0;
        self.kaishiButton.alpha = 1;
        self.jieShuButton.alpha = 1;
        
        self.kaishiButton.frame = CGRectMake(87*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 80*CHANGE_SIZE - 80*CHANGE_SIZE, 80*CHANGE_SIZE, 80*CHANGE_SIZE);
        self.dingWeiButton.frame = CGRectMake(25*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 97.5*CHANGE_SIZE - 45*CHANGE_SIZE, 45*CHANGE_SIZE, 45*CHANGE_SIZE);
        
        self.jieShuButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2+25*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 80*CHANGE_SIZE - 80*CHANGE_SIZE, 80*CHANGE_SIZE, 80*CHANGE_SIZE);
        
        self.zhaoXiangButton.frame = CGRectMake(DEF_SCREEN_WIDTH-25*CHANGE_SIZE-45*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 97.5*CHANGE_SIZE - 45*CHANGE_SIZE, 45*CHANGE_SIZE, 45*CHANGE_SIZE);
        
        
    } completion:^(BOOL finished) {
//        self.zanTingButton.hidden = YES;
//        self.kaishiButton.hidden = NO;
        
        
    }];
    
    
}
//开始
- (void)kaishiClick{
    NSLog(@"111111111");
    [UIView animateWithDuration:0.3 animations:^{
        
        self.zanTingButton.alpha = 1;
        self.kaishiButton.alpha = 0;
        self.jieShuButton.alpha = 0;
        
        self.zanTingButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2-50*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 70*CHANGE_SIZE - 100*CHANGE_SIZE, 100*CHANGE_SIZE, 100*CHANGE_SIZE);
        
        self.dingWeiButton.frame = CGRectMake(45*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 90*CHANGE_SIZE - 60*CHANGE_SIZE, 60*CHANGE_SIZE, 60*CHANGE_SIZE);
        self.zhaoXiangButton.frame = CGRectMake(DEF_SCREEN_WIDTH- 45*CHANGE_SIZE-60*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 90*CHANGE_SIZE - 60*CHANGE_SIZE, 60*CHANGE_SIZE, 60*CHANGE_SIZE);
        
        self.jieShuButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2, DEF_SCREEN_HEIGHT - 80*CHANGE_SIZE - 80*CHANGE_SIZE, 80*CHANGE_SIZE, 80*CHANGE_SIZE);
        self.kaishiButton.frame = CGRectMake(DEF_SCREEN_WIDTH/2-80*CHANGE_SIZE, DEF_SCREEN_HEIGHT - 80*CHANGE_SIZE - 80*CHANGE_SIZE, 80*CHANGE_SIZE, 80*CHANGE_SIZE);
        
    } completion:^(BOOL finished) {
//        self.zanTingButton.hidden = NO;
//        self.kaishiButton.hidden = YES;
        
        
    }];
    
    
    
}
- (void)jieShuClick{
    
    [self.locationManager stopUpdatingLocation];
    [self.navigationController popViewControllerAnimated:YES];
                                                                                                                                          
}
- (void)dingWeiClick{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
